<template>
  <el-dialog title="新增员工" :visible="showDialog" @close="btnCancel">
    <!-- 表单 -->
    <el-form ref="addEmployee" label-width="120px" :model="formEmployee" :rules="rules">
      <el-form-item label="姓名" prop="username">
        <el-input v-model="formEmployee.username" style="width:90%" placeholder="请输入姓名" />
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input v-model="formEmployee.mobile" style="width:90%" placeholder="请输入手机号" />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker v-model="formEmployee.timeOfEntry" style="width:90%" placeholder="请选择入职时间" />
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select v-model="formEmployee.formOfEmployment" style="width:90%" placeholder="请选择">
          <el-option
            v-for="item in employeeEnum.hireType"
            :key="item.id"
            :value="item.id"
            :label="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input v-model="formEmployee.workNumber" style="width:90%" placeholder="请输入工号" />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <el-input v-model="formEmployee.departmentName" style="width:90%" placeholder="请选择部门" @focus="showTree=true" />
        <el-tree
          v-if="showTree"
          class="deptsTree"
          :data="depts"
          :props="{label:'name'}"
          default-expand-all
          @node-click="selectFn"
        />
      </el-form-item>
      <el-form-item label="转正时间" prop="correctionTime">
        <el-date-picker v-model="formEmployee.correctionTime" style="width:90%" placeholder="请选择转正时间" />
      </el-form-item>
    </el-form>
    <!-- footer插槽 -->
    <template v-slot:footer>
      <el-row type="flex" justify="center">
        <el-col :span="6">
          <el-button size="small" class="btnCancel" @click="btnCancel">取消</el-button>
          <el-button type="primary" size="small" @click="btnOk">确定</el-button>
        </el-col>
      </el-row>
    </template>
  </el-dialog>
</template>

<script>
// 枚举数据聘用形式
import employeeEnum from '@/api/constant/employees'
// 部门api
import { getDepartments } from '@/api/departments'
// 引入查找树形函数
import { listToTreeData } from '@/utils'
// 新增员工接口
import { addEmployee } from '@/api/employees'
export default {
  props: {
    // 弹窗
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      // 新增员工表单
      formEmployee: {
        mobile: '', // 手机
        username: '', // 名称
        timeOfEntry: '', // 入职时间
        formOfEmployment: '', // 聘用形式
        correctionTime: '', // 转正日期
        workNumber: '', // 工号
        departmentName: '' // 部门
      },
      // 枚举数据 聘用形式
      employeeEnum,
      // 部门
      depts: [],
      // 隐藏显示部门
      showTree: false,
      // 表单规则
      rules: {
        // 名字
        username: [{ required: true, message: '用户姓名不能为空', trigger: 'blur' }, {
          min: 1, max: 4, message: '用户姓名为1-4位'
        }],
        // 手机号
        mobile: [{ required: true, message: '手机号不能为空', trigger: 'blur' }, {
          pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: 'blur'
        }],
        // 聘用形式
        formOfEmployment: [{ required: true, message: '聘用形式不能为空', trigger: 'blur' }],
        // 工号
        workNumber: [{ required: true, message: '工号不能为空', trigger: 'blur' }],
        // 部门
        departmentName: [{ required: true, message: '部门不能为空', trigger: 'change' }],
        // 入职时间
        timeOfEntry: [{ required: true, message: '入职时间', trigger: 'blur' }]
      }
    }
  },
  created() {
    // 部门
    this.getDepartments()
  },
  methods: {
    // 通知父组件关闭弹窗
    btnCancel() {
      // 清空表单
      this.formEmployee = {
        mobile: '', // 手机
        username: '', // 名称
        timeOfEntry: '', // 入职时间
        formOfEmployment: '', // 聘用形式
        correctionTime: '', // 转正日期
        workNumber: '', // 工号
        departmentName: '' // 部门
      }
      // 清空校验表单报错
      this.$refs.addEmployee.resetFields()
      // 关闭弹窗
      this.$emit('update:showDialog', false)
    },
    // 部门获取
    async getDepartments() {
      // 部门
      const { depts } = await getDepartments()
      // 查找树形部门
      this.depts = listToTreeData(depts, '')
    },
    // 树形部门点击 赋值
    selectFn(dept) {
      // 赋值部门
      this.formEmployee.departmentName = dept.name
      // 关闭树形
      this.showTree = false
    },
    // 确认增加员工
    async btnOk() {
      // 表单校验
      await this.$refs.addEmployee.validate()
      // 发送请求
      await addEmployee(this.formEmployee)
      // 提示用户
      this.$message.success('新增成功')
      // 更新
      this.$parent.getEmployee()
      // 关闭弹窗
      this.$parent.showDialog = false
    }
  }
}
</script>

<style lang="scss" scoped>
.deptsTree{
  width: 90%;
  height: 200px;
  border: 1px solid #ccc;
  overflow: auto;
  position: absolute;
  z-index: 9;
}
</style>
